package com.ccb.sc.groupon.controller;

import com.ccb.sc.auth.TokenAuth;
import com.ccb.sc.auth.TokenModel;
import com.ccb.sc.groupon.query.AppGrouponSearchParam;
import com.ccb.sc.groupon.service.AppGrouponService;
import com.ccb.sc.groupon.vo.AppGrouponListVo;
import com.ccb.sc.groupon.vo.GrouponLogListVo;
import com.ccb.sc.groupon.vo.GrouponStatVo;
import com.ccb.sc.groupon.vo.GrouponVo;
import com.ccb.sc.common.bind.annotation.CurrentUser;
import com.ccb.sc.common.bind.annotation.Limit;
import com.ccb.sc.common.bind.page.PageParams;
import com.ccb.sc.common.bind.page.PageVo;
import com.ccb.sc.common.bind.response.ResBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author ken
 * @version 1.0
 * @date 2020-09-21
 */
@RestController
@Api(value = "团购管理", tags = "团购管理")
@RequestMapping("/app/groupon")
public class AppGrouponController {

    @Autowired
    private AppGrouponService grouponService;

    @Limit(period = 10, count = 10)
    @GetMapping("/list")
    @ApiOperation(value = "首页团购列表", response = AppGrouponListVo.class, responseContainer = "list")
    public ResBody<?> list(@Validated AppGrouponSearchParam queryParam) {
        PageVo<AppGrouponListVo> page = this.grouponService.list(queryParam);
        return ResBody.success(page);
    }

    @Limit(period = 10, count = 10)
    @GetMapping("/goodsId/{goodsId}")
    @ApiOperation(value = "团购详情", response = GrouponVo.class)
    public ResBody<?> list(@ApiIgnore @CurrentUser TokenModel token, @PathVariable Long goodsId) {
        Long memberId = token != null ? token.getId() : null;
        GrouponVo vo = this.grouponService.findByGoodsId(memberId, goodsId);
        return ResBody.success(vo);
    }

    @TokenAuth
    @GetMapping("/stat")
    @ApiOperation(value = "团购统计", response = GrouponStatVo.class)
    public ResBody<?> stat(@ApiIgnore @CurrentUser TokenModel token) {
        GrouponStatVo statVo = this.grouponService.stat(token.getId());
        return ResBody.success(statVo);
    }

    @TokenAuth
    @GetMapping("/records")
    @ApiOperation(value = "我的团购记录", response = GrouponLogListVo.class, responseContainer = "list")
    public ResBody<?> records(PageParams queryParam, @ApiIgnore @CurrentUser TokenModel token) {
        PageVo<GrouponLogListVo> page = this.grouponService.getRecords(token.getId(), queryParam);
        return ResBody.success(page);
    }

    @TokenAuth
    @GetMapping("/inviteRecords")
    @ApiOperation(value = "我的分享团购记录", response = GrouponLogListVo.class, responseContainer = "list")
    public ResBody<?> inviteRecords(PageParams queryParam, @ApiIgnore @CurrentUser TokenModel token) {
        PageVo<GrouponLogListVo> page = this.grouponService.getInviteRecords(token.getId(), queryParam);
        return ResBody.success(page);
    }


}
